package dpsscorbainterface;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

import org.omg.CORBA.ORB;


public class PlayerClient extends Thread {
		String serverName;
        BufferedWriter write ;
        FileWriter writer ;

	public static void main(String[] args) throws IOException {
	ORB orb = ORB.init(args,null);
		
		BufferedReader br1 = new BufferedReader(new FileReader("ior1.txt"));
		String ior1 = br1.readLine();
		br1.close();
		
		org.omg.CORBA.Object o1 = orb.string_to_object(ior1);
		corbainterface NorthAmerica = corbainterfaceHelper.narrow(o1);
		
		BufferedReader br2 = new BufferedReader(new FileReader("ior2.txt"));
		String ior2 = br2.readLine();
		br2.close();
		
		org.omg.CORBA.Object o2 = orb.string_to_object(ior2);
		corbainterface Europe = corbainterfaceHelper.narrow(o2);
		
		
		BufferedReader br3 = new BufferedReader(new FileReader("ior3.txt"));
		String ior3 = br3.readLine();
		br3.close();
		
		org.omg.CORBA.Object o3 = orb.string_to_object(ior3);
		corbainterface Asia = corbainterfaceHelper.narrow(o3);
		
		
		
		int choice;
		try{
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			System.out.println("Let's Begin!");                        
			do
			{

				System.out.println("1. Create Game account from system");
                System.out.println("2. Sign In");
                System.out.println("3. Create new Game account ");
				System.out.println("4. Sign Out");
				System.out.println("5. Transfer Account");
                System.out.println("6. Exit");
				choice = Integer.parseInt(br.readLine());
					
					switch(choice)
					{
					
					case 1:
						System.out.println("Your account has been created at each station");
						
						NorthAmerica.createPlayerAccount("A","B",(short) 22,"a","b","132.32.32.32");
						Europe.createPlayerAccount("C","D",(short) 23,"c","d","93.32.32.32");
						Asia.createPlayerAccount("E","F",(short) 24,"e","f","182.32.32.32");
						NorthAmerica.createPlayerAccount("G","H",(short) 25,"g","h","132.32.32.32");
						Europe.createPlayerAccount("I","J",(short) 26,"i","j","93.32.32.32");
						Asia.createPlayerAccount("K","L",(short) 27,"k","l","182.32.32.32.32");
						NorthAmerica.createPlayerAccount("M","N",(short) 28,"m","n","132.32.32.32");
						Europe.createPlayerAccount("O","P",(short) 29,"o","p","93.32.32.32");
						Asia.createPlayerAccount("Q","R",(short) 30,"q","r","182.32.32.32");
						NorthAmerica.createPlayerAccount("S","T",(short) 31,"s","t","132.32.32.32");
						NorthAmerica.playerSignIn("a","b","132.32.32.32");
						NorthAmerica.playerSignIn("g","h","132.32.32.32");
						Europe.playerSignIn("c","d","93.32.32.32");
						Asia.playerSignIn("e","f","182.32.32.32");
						break;
						
					
					case 2:
                        System.out.println("Enter the username and password : " );
                        Scanner sc = new Scanner(System.in);
                        String Username = sc.next();
                        String Password = sc.next();
                        System.out.println("Enter the IP Address : " );
                        String ipp = sc.next();
                        
                        String ans = "" ;
                        if(ipp.startsWith("132")){
                        	ans  = NorthAmerica.playerSignIn(Username, Password, ipp);
                             ans = ans + "From North America" ;
                   	
                        }else if(ipp.startsWith("93")){
                        	ans = Europe.playerSignIn(Username, Password, ipp);
                        	  ans = ans + "From Europe" ;
                        }else if(ipp.startsWith("182")){
                        	ans = Asia.playerSignIn(Username, Password, ipp);
                        	  ans = ans + "From Asia" ;
                        }
                      
                        System.out.println("Ans:"+ ans);
               		break;
						
                    case 3:
                        Scanner scanner = new Scanner(System.in);
                        System.out.println("Enter First Name: ");
                        String First = scanner.next();
                        System.out.println("Enter Last name: ");
                        String Last  = scanner.next() ;
                        System.out.println("Enter the Age: ");
                        short age = (short) Integer.parseInt(scanner.next());
                        System.out.println("Enter the Username: ");
                        String usename = scanner.next();
                        if(usename.length() < 6 ) {
                        	System.out.println("Username Can't be less than 6 characters. Please start again!");
                            break;
                        }
                        if (usename.length() > 15) {
                        	System.out.println("Username Can't be more than 15 characters. Please start again!");
                            break;
                        }
                        	
                        System.out.println("Enter the Password: ");
                        String password = scanner.next();
                        if(password.length() < 6){
                        	System.out.println("Password Can't be less than 6 characters. Please start again!");
                            break;
                        }
                        	
                        
                        System.out.println("Enter the  IP Address: ");
                        String scan = scanner.next();
                        
                      
                        if(scan.startsWith("132")){
                                 NorthAmerica.createPlayerAccount(First, Last, age, usename, password, scan);
                                 
                                   System.out.println("Account has been created for North America");
                        }else if(scan.startsWith("93")){
                       Europe.createPlayerAccount(First, Last, age, usename, password, scan);
                            
                        	System.out.println("Account has been created for Europe");
                        }else if(scan.startsWith("182")){
                         Asia.createPlayerAccount(First, Last, age, usename, password, scan);
                            
                        	System.out.println("Account has been created for Asia");
                        }
                        
                        else{
                        	System.out.println("Username already exist: Please try again!");
                        	
                        }
                         break;
						
					case 4: 
                           System.out.println("Enter the username and IPAddress" );
                           Scanner scc = new Scanner(System.in);
                           String username = scc.next();
                           String IP = scc.next();
                           if (IP.startsWith("132")){
    						   String ansew = NorthAmerica.playerSignOut(username, IP);
    						   System.out.println("Ans:"+ ansew);}
    						  else if (IP.startsWith("93")){
    							   String ansew = Europe.playerSignOut(username, IP);
    							   System.out.println("Ans:"+ ansew);}
    						   else{
    							   String ansew = Asia.playerSignOut(username, IP);
    							   System.out.println("Ans:"+ ansew);}
                          break;
					
					case 5:
						   System.out.println("Enter the username and Password" );
						   Scanner scca = new Scanner(System.in);
                           String usernamme = scca.next();
                           String passwordd = scca.next();
						   System.out.println("Enter the old IPAddress" );
						   String oip = scca.next();
						   System.out.println("Enter the new IPAddress" );
						   String nip = scca.next();
						   if (oip.startsWith("132")){
						   String ansew = NorthAmerica.transferAccount(usernamme, passwordd, oip, nip);
						   System.out.println("Ans:"+ ansew);}
						  else if (oip.startsWith("93")){
							   String ansew = Europe.transferAccount(usernamme, passwordd, oip, nip);
							   System.out.println("Ans:"+ ansew);}
						   else{
							   String ansew = Asia.transferAccount(usernamme, passwordd, oip, nip);
							   System.out.println("Ans:"+ ansew);}
						   break;  
					case 6: 
						System.out.println("Cioa!");
						System.exit(0);
						break;
					
					default:
						System.out.println("Invalid selection:");
						break;
					}
					
				} while(choice != 6);
		}
		catch (Exception ex){
			ex.printStackTrace();
		}	
	}
}
